package dice.sinanya.tools.makedata;

import dice.sinanya.entity.EntityAllReply;
import dice.sinanya.entity.EntityGroupSwitch;
import dice.sinanya.entity.EntityReply;
import dice.sinanya.entity.EntityTypeMessages;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static dice.sinanya.system.MessagesBanList.groupSwitchHashMap;
import static dice.sinanya.system.MessagesSystem.AllReply;
import static dice.sinanya.system.MessagesSystem.replyMap;
import static dice.sinanya.tools.getinfo.LogTag.checkOthorLogTrue;
import static dice.sinanya.tools.makedata.RandomInt.random;
import static dice.sinanya.tools.makedata.Sender.sender;

public class ReplyTool {

    public static ArrayList<EntityAllReply> makeReplyForTable() {
        ArrayList<EntityAllReply> allRepliesList = new ArrayList<>();
        for (Map.Entry<Long, HashMap<String, ArrayList<EntityReply>>> allReplies : AllReply.entrySet()) {
            for (Map.Entry<String, ArrayList<EntityReply>> replies : allReplies.getValue().entrySet()) {
                for (EntityReply entityReply : replies.getValue()) {
                    EntityAllReply entityAllReply = new EntityAllReply();
                    entityAllReply.setQq(allReplies.getKey());
                    entityAllReply.setKey(replies.getKey());
                    entityAllReply.setValue(entityReply.getValue());
                    allRepliesList.add(entityAllReply);
                }
            }
        }
        return allRepliesList;
    }

    public static void reply(EntityTypeMessages entityTypeMessages) {
        String msg = entityTypeMessages.getMsgGet().getMsg().trim();
        if (!replyMap.containsKey(msg)) {
            return;
        }
        EntityGroupSwitch entityGroupSwitch = groupSwitchHashMap.getOrDefault(entityTypeMessages.getFromGroup(), new EntityGroupSwitch());
        if (!entityGroupSwitch.isReply() || entityGroupSwitch.isSimple() || checkOthorLogTrue(entityTypeMessages.getFromGroupString())) {
            return;
        }
        for (String tmp : makeFormat(randomReply(msg))) {
            sender(entityTypeMessages, tmp);
        }
    }

    private static String randomReply(String msg) {
        ArrayList<EntityReply> replies = replyMap.get(msg);
        ArrayList<EntityReply> randomList = new ArrayList<>();
        for (EntityReply entityReply : replies) {
            for (int i = 0; i < entityReply.getWeight(); i++) {
                randomList.add(entityReply);
            }
        }
        return randomList.get(random(0, randomList.size() - 1)).getValue();
    }

    private static ArrayList<String> makeFormat(String msg) {
        ArrayList<String> result = new ArrayList<>();
        if (msg.contains("\\r")) {
            result = new ArrayList<>(Arrays.asList(msg.split("\\\\r")));
        }else{
            result.add(msg);
        }
        return (ArrayList<String>) result.stream().parallel().map(ReplyTool::makeFormatEvery).collect(Collectors.toList());
    }

    private static String makeFormatEvery(String msg) {
        return msg.replace("\\n", "\n").replace("\\t", "\t");
    }
}
